package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;	

import com.mysql.jdbc.Statement;
	
public class GrabarReserva  extends HttpServlet{


		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			
			

			String idpqt = req.getParameter("idpqt");//paquete.
			String idvuelo = req.getParameter("idvuelo");//que vuelo es
			int nitamos = Integer.parseInt(req.getParameter("cantcl"));//asientos q nitamos
			String strnitamos = req.getParameter("cantcl");			
			String[] clientes =req.getParameter("clientesC").split("|");
			String clientesConcat =req.getParameter("clientesC");
			java.sql.Connection con;
			String idReserva = "-1"; //id de la reserva
			
			ResultSet rs;
						
			
			String btnvolver =  "<input type='button' onclick='location.href=\"Index.jsp\"' value='Volver' />";
			String databaseName = "grupo_1_db";
		  try {
			Class.forName("com.mysql.jdbc.Driver"); 
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + databaseName,"root","ignacio"); 
			Statement st1= (Statement) con.createStatement(); 		
			Statement st2= (Statement) con.createStatement(); 
			Statement st3= (Statement) con.createStatement(); 
			rs=st1.executeQuery("SELECT ifnull(max(idreservas),0) as max FROM reservas;");	
		
			
			 while(rs.next()) 
				{ 	
					
					idReserva = String.valueOf(Integer.parseInt(rs.getString(1)) + 1);//sigt id
					
					
				}
					
			 
			 
					 
		String querydescontarAsientos = "update vuelos set asientosDisponibles=asientosDisponibles - "+ strnitamos + " where idvuelo = idvuelo";
	     st2.executeUpdate(querydescontarAsientos);	
			 
		String queryppal = "insert into reservas (idreservas,idpaquete,idvuelo) values(" + idReserva + "," + idpqt + "," + idvuelo + ")";		
		st2.executeUpdate(queryppal);	
				
		
			for (int i=0;i<clientes.length;i++) {				
				
				String querytemp = "insert into reservas_personas (idreserva,idcliente) values(" + idReserva + "," +  clientes[i] + ")"; 
				
				response(req,resp, "<br>"+querytemp);
				
				Statement stemp= (Statement) con.createStatement(); 
				stemp.executeUpdate(querytemp);							           
		    }
						        
		
		
		
			req.setAttribute("message", "Se pudo guardar en la base de datos su reserva. Nro. Reserva" + idReserva); // 			   
			req.setAttribute("btnfinal", btnvolver);
		    req.getRequestDispatcher("/GuardarReserva.jsp").forward(req, resp);
		
			
			  if(con!=null) {con.close();}
			
		  }
			catch (Exception e)
			{
				response(req,resp, "Error en la conexion con la base de datos al grabar la reserva");
			}
		  
		  
		 
	
		  
			
			//vueloasignado id
		
			//response(resp, msjdevolver);
			
			//if ("edu4java".equals(user) && "eli4java".equals(pass)) {
			//	response(resp, "login ok");
			//} else {
			//	response(resp, "invalid login");
			//}
		}

		private void response(HttpServletRequest req,HttpServletResponse resp, String msg)
				throws IOException, ServletException {
			PrintWriter out = resp.getWriter();
			out.println("<html>");
			out.println("<body>");
			
			//out.println("<script>alert(1);</script>");
			out.println("<t1>" + msg + "</t1>");
			out.println("</body>");
			out.println("</html>");
			
			  // String message = "Hello World";
			 //  req.setAttribute("message", msg); // This will be available as ${message}
		      //  req.getRequestDispatcher("/ValidarReserva.jsp").forward(req, resp);
		 

			
		}
		

}
